package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entity.Dish;
import com.itheima.entity.DishDto;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: 宋H
 * @Date: 2022/9/6
 * @Description: com.itheima.service.impl
 * @version: 1.0
 */
@Transactional
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    public void saveList(DishDto dishDto) {
        //首先保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //保存菜品口味数据到菜品口味表dish_flavor
        //获取dish菜品的Id
        Long dishid = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishid);//通过菜品id设置口味ID
        }

      /*  flavors = flavors.stream().map((item) -> {
            item.setDishId(dishid);
            return item;
        }).collect(Collectors.toList());*/
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据菜品id获取菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getDishFlavorById(Long id) {
        //根据菜品id,获取菜品信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        //拷贝菜品表到dishDato对象中,以便通过dishDto携带dish表的信息一起查出口味表的信息
        BeanUtils.copyProperties(dish,dishDto);

        //根据菜品id，获取菜品口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id!=null,DishFlavor::getDishId,dish.getId());

        //根据菜品的id和口味id相同是，查询出口味信息，存入dishDto对象Flavors数组中
        List<DishFlavor> list = dishFlavorService.list(lqw);
        dishDto.setFlavors(list);
        return dishDto;
    }


    /**
     * 修改菜品信息，同时更新对应的口味信息 
     * @param dishDto
     * @return 
     */
    @Override
    public void dishUpadate(DishDto dishDto) {
        //更新dish表信息
        this.updateById(dishDto);


        //首先清除当前菜品信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());//主要dishDto中的id实际上就是dish表的id（继承）
        dishFlavorService.remove(lqw);

        //添加 dishFlavor表信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 修改菜品售卖状态
     * @param status
     * @param ids
     */
    @Override
    public void dishStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();

        lqw.in(ids!=null,Dish::getId,ids);
        List<Dish> list = this.list(lqw);
        for (Dish dish : list) {
            if(dish!=null){
                dish.setStatus(status);
                this.updateById(dish);
            }
        }
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void dishRemove(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids!=null,Dish::getId,ids);
        List<Dish> dishList = this.list(lqw);
        for (Dish dish : dishList) {
            if(dish!=null&&dish.getStatus()!=0){
                this.removeById(dish);
            }else{
                throw new CustomException("要删除的菜品中有正在售卖的菜品，无法删除");
            }
        }
    }
}
